Skip to content

Conversation

@bjackman
Copy link

Docopts also includes a shell wrapper. This can be sourced, which provides a slightly more ergonomic interface. More importantly though, this is what the examples in the docopts repository use, so it's much more accessible.

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
    • I checked that this allows a writeShellApplication script to source docopts.sh
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • Nixpkgs 25.11 Release Notes (or backporting 24.11 and 25.05 Nixpkgs Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
  • NixOS 25.11 Release Notes (or backporting 24.11 and 25.05 NixOS Release notes)
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@NixOSInfra NixOSInfra added the 12.first-time contribution This PR is the author's first one; please be gentle! label May 17, 2025
@bjackman
Copy link
Author

I believe this change is suitable for backports, if you agree and you're a maintainer I'd appreciate if you could add the backport labels :)

@github-actions github-actions bot added 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 1 This PR causes 1 package to rebuild on Linux. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. labels May 17, 2025
@nix-owners nix-owners bot requested a review from con-f-use May 17, 2025 22:25
bjackman added a commit to bjackman/nixos-flake that referenced this pull request May 30, 2025
Not sure if NixOS/nixpkgs#408168 will get
reviewed.
@bjackman
Copy link
Author

bjackman commented Jun 2, 2025

Er, hold up a minute...

{
  description = "A very basic flake";

  inputs = {
    nixpkgs.url = "github:bjackman/nixpkgs?ref=docopts-sh";
  };

  outputs = { self, nixpkgs }: {
    packages.x86_64-linux.default =
      let pkgs = import nixpkgs { system = "x86_64-linux"; };
      in pkgs.writeShellApplication {
        name = "hello";
        runtimeInputs = [ pkgs.docopts ];
        text = "source docopts.sh";
      };
  };
}
❯❯  nix build
warning: creating lock file '/tmp/nix-docopts-test/flake.lock': 
• Added input 'nixpkgs':
    'github:bjackman/nixpkgs/9f8d82883e6df7ca8634ddb291e7330c36309f67?narHash=sha256-iKknSQ583R9o0t9X/x18wEfZRCcXqhpWYeeY97jmaGw%3D' (2025-05-17)
error: builder for '/nix/store/dc9x9js9d1x399bv4alfdn5vm7g46inw-hello.drv' failed with exit code 1;
       last 7 log lines:
       >
       > In /nix/store/48j8rfz1xk0ihs25j35rc1kqagldc3dk-hello/bin/hello line 8:
       > source docopts.sh
       >        ^--------^ SC1091 (info): Not following: docopts.sh was not specified as input (see shellcheck -x).
       >
       > For more information:
       >   https://www.shellcheck.net/wiki/SC1091 -- Not following: docopts.sh was not...
       For full logs, run 'nix-store -l /nix/store/dc9x9js9d1x399bv4alfdn5vm7g46inw-hello.drv'.

Seems I did not test this properly before.

@bjackman
Copy link
Author

bjackman commented Jun 2, 2025

Ah right yeah this is just a foible of writeShellApplication and shellcheck. Turns out shellcheck wants to read the files you source too so you have to make those available to it.

So to take advantage of this you do need to use some different logic for wrapping up the script, or you need to extend writeShellApplication. But this PR does do what it's supposed to do.

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/prs-ready-for-review/3032/5676

@bjackman bjackman force-pushed the docopts-sh branch 2 times, most recently from c1dda74 to 865991b Compare October 4, 2025 13:51
Docopts also includes a shell wrapper. This can be sourced, which
provides a slightly more ergonomic interface. More importantly
though, this is what the examples in the docopts repository use, so
it's much more accessible.

Signed-off-by: Brendan Jackman <bhenryj0117@gmail.com>
@bjackman
Copy link
Author

bjackman commented Oct 4, 2025

Oops, fixed the formatting.

@con-f-use
Copy link
Contributor

con-f-use commented Dec 2, 2025

lgtm. Although it seems, that just adding another patch to the patches = [ ... array would be a cleaner solution since bash files are aware of their own source path and you could add to path relative to that.

@nixpkgs-ci nixpkgs-ci bot added 12.approvals: 1 This PR was reviewed and approved by one person. 12.approved-by: package-maintainer This PR was reviewed and approved by a maintainer listed in any of the changed packages. labels Dec 2, 2025
@bjackman bjackman requested a review from dramforever December 7, 2025 01:22
{ print }' docopts.sh
# Now, since this will be sourced by the user and we don't want to clobber
# PATH in their scripts, add a line at the end to restore the old PATH.
echo 'PATH="$__NIX_OLD_PPATH"' >> docopts.sh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... it is a bit cursed. Does this really work? docopts.sh exports several functions like docopt_get_raw_value which calls awk. Resetting PATH at the end of the script probably breaks that, right?

I'm not sure I have a clean solution. I'd say maybe just append to the PATH only, and don't restore at the end?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes thank you you are right. I only thought about the wrapper mode but this will break the library mode.

This kinda just seems like an incompatiility between the "yolo everything is global" model of Bash and the principles of Nix.

I don't really like clobbering people's path when they source the script... In fact, since I first wrote this PR, I decided I also don't really like docopts all that much. So much so that maybe I'd rather not package docopts.sh at all, than package it with the "clobbers $PATH" issue... I will ponder it and get back to you!

(Google has an internal Bash library for arg parsing in the style of Abseil/Go that I like much more. It's much much simpler. Let me know if you know of anything like this for Bash! Otherwise maybe I'll try to make an open source version...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 10.rebuild-linux: 1 This PR causes 1 package to rebuild on Linux. 12.approvals: 1 This PR was reviewed and approved by one person. 12.approved-by: package-maintainer This PR was reviewed and approved by a maintainer listed in any of the changed packages. 12.first-time contribution This PR is the author's first one; please be gentle!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants